<?
/**
**江西瑞金
**2015-05-29
**User by pzp
**/
class RunJIN{
	private $USER = 'PFTORDER'; 
	private $PWD = 'PFTORDER';
	private $submit_url = 'http://121.42.56.222/et/ebusinessTC/submitOrder.do';
	private $select_url = 'http://121.42.56.222/et/ebusinessTC/orderInfo.do';
	private $remove_url = 'http://121.42.56.222/et/ebusinessTC/removeOrder.do';
	public function __construct($lid){
		switch ($lid){
			case 8054:  //红井
				$this->BARCODES= 'HJPFT';
				$this->ticket_url = 'http://61.180.70.105:81/et/ebusinessTC/ticketInfo.do';
			break;
			case 8055:  //二苏大
				$this->BARCODES= 'ESPFT';
				$this->ticket_url = 'http://61.180.70.102:81/et/ebusinessTC/ticketInfo.do';
			break;
			case 8048:  //叶坪
				$this->BARCODES= 'YPPFT';
				$this->ticket_url = 'http://61.180.70.99:81/et/ebusinessTC/ticketInfo.do';
			break;
		}
	}
	
	//票类查询
	public function ticketInfo(){
		$post_array = array();
		$ticketInfo_array = $this->RJ_POST($post_array,$this->ticket_url);
		return $ticketInfo_array;
	} 
	
	//订单提交
	public function submitOrder($request_array){
		$time = time();
		for($i=0;$i<$request_array['UUtnum'];$i++){
			$BARCODE = $this->BARCODES.$time.$i;
			$BARCODEs[].= $BARCODE;
		}	
		$BARCODEss = json_encode($BARCODEs);
		$post_array = array(
			'ORDERID'		=> $request_array['pftordernum'], 						//电商自己的订单号
			'TICKETORDERID'	=> $request_array['pftordernum'], 						//电商票类订单号
			'NUMBER'		=> $request_array['IDcar'], 							//取票人身份证
			'NAME'			=> $request_array['pftordername'], 						//取票人姓名
			'MOBILE'		=> $request_array['UUordertel'], 						//取票手机号码
			'CUSTOMERNAME'	=> '票付通', 											//客户名称（旅行社名称）
			'SOURCE'		=> '票付通', 											//客源地
			'BARCODES'		=> $BARCODEss, 											//条码集合
			'COUNT'			=> $request_array['UUtnum'], 							//人数
			'PRICE'			=> $request_array['UUtprice'], 							//单价
			'AMOUNT'		=> $request_array['UUtnum'] * $request_array['UUtprice'],//总金额
			'BEGINDATE'		=> date('Ymd',strtotime($request_array['UUordertime'])),//有效期(起始)格式：yyyyMMddHHmmss
			'ENDDATE'		=> str_replace('-','',$request_array['UUendtime']), 	//有效期(截止)
			'TICKETUUID'	=> $request_array['uuid'], 								//票类编号
			'TYPE'			=> 1, 													//是否一票多人
			'NETWORK'		=> '票付通',  											//预定网站名称
			'ORDER_STATE'	=> 0, 													//订单状态
			'ORDERDATE'		=> date('YmdHis',strtotime($request_array['UUordertime'])), //订单时间
			'ORDER_TYPE'	=> '票付通', 											//订单类型
			'VIEWNAME'		=> $request_array['UUltitle'], 							//景区名称
			'TICKETNAME'	=> $request_array['productname'], 						//票类名称
			'OPERATERNAME'	=> $this->USER, 										//操作员名称
			'STATE'			=> $request_array['ispay'],
			'QRCODE'		=> $request_array['UUcode']
		);
		$submitOrder_array = $this->RJ_POST($post_array,$this->submit_url);
		if($submitOrder_array['content']['code'] == 1){
			return 100;
		}else{
			$error_msg = $submitOrder_array['content']['message'];
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.$request_array['pftordernum'].'|'.$error_msg.'|'.json_encode($post_array));
			return 101;
		}
	}
	
	//订单查询 
	public function orderInfo(){
		$post_array = array(
			'ORDERID'=>'0601001', //电商自己的订单号
			'TICKETORDERID'=>'601001', //电商票类订单号
		);
		print_r($post_array);
		$orderInfo_array = $this->RJ_POST($post_array,$this->select_url);
		return $orderInfo_array;			
	}
	
	//订单撤销
	public function removeOrder($order_true){
		$post_array = array(
			'ORDERID'=>$order_true, 		//电商自己的订单号
		);
		$removeOrder_array = $this->RJ_POST($post_array,$this->remove_url);
		if($removeOrder_array['content']['code'] == 1){
			return 100;
		}else{
			$this->logfile(json_encode($removeOrder_array));
			$error_msg = $removeOrder_array['content']['message'];
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.$order_true.'|'.$error_msg);
			return 101;
		}
	}
	
	//post
	public function RJ_POST($post_array,$url){
		$o=""; 
		foreach ($post_array as $k=>$v){ 
			$o.= "$k=".urlencode($v)."&"; 
		} 
		$post_array=substr($o,0,-1); 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_HEADER, 0); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL,$url); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array); 
		$reponse = curl_exec($ch);
		curl_close($ch);
		if($reponse){
			return json_decode($reponse,true);			
		}else{
			return $reponse;
		}
	}
	
	//日志
	// public function logfile($txt){
		// $fp = fopen('/var/www/html/new/d/module/api/RuiJin/ruijin_logfile.txt',"a");
		// flock($fp, LOCK_EX) ;
		// fwrite($fp,$txt."\n");
		// flock($fp, LOCK_UN);
		// fclose($fp); 
	// }
	
	public function logfile($word){
		$path = 'ruijin_logfile';
		$filepath= '/var/www/html/new/d/logs/api/RuiJin/'.$path.'.txt';
		if(file_exists($filepath)){
			$word = date("Y-m-d H:i:s")."|".$word;
			// $word = mb_convert_encoding($word, 'GBK', 'UTF-8');
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
			{
				rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
				$handler=fopen($filepath,'w');
				flock($handler, LOCK_EX) ;
				fwrite($handler,"\n".$word);
				flock($handler, LOCK_UN);
				fclose($handler);
			}
			else
			{
				$fp = fopen('/var/www/html/new/d/logs/api/RuiJin/'.$path.'.txt',"a");
				flock($fp, LOCK_EX);
				fwrite($fp,$word."\n");
				flock($fp, LOCK_UN);
				fclose($fp);
			}
		}else{
			$fp = fopen('/var/www/html/new/d/logs/api/RuiJin/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}
}
// $lid = 8054;
// $RJ = new RunJIN($lid);
// $get_ticketInfo = $RJ->ticketInfo();print_r($get_ticketInfo);
// $submit_order = $RJ->submitOrder();print_r($submit_order);
// $orderInfo_order = $RJ->orderInfo();print_r($orderInfo_order);
// $removeOrder_order = $RJ->removeOrder();print_r($removeOrder_order);
 
?>